const Router = require('koa-router')
const glob = require('glob')
const path = require('path')
const compose = require('koa-compose')

const log4 = require('../utils/log4')()

let files = glob.sync(path.resolve(__dirname, './*/*.js'))
let routes = compose(
	files.map((filePath) => {
		let router = require(filePath)
		// 如果不上Router实例 抛出异常
		if (!(router instanceof Router)) {
			log4.debug('router load fail')
			throw new Error(`${filePath} need export koa-router instance`)
		}
		return router.routes()
	})
)

log4.info('router load done..')

module.exports = routes
